package cn.edu.sgu.www.mhxysy.service.xingshi.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.xingshi.XingshiDTO;
import cn.edu.sgu.www.mhxysy.dto.xingshi.XingshiInlayDTO;
import cn.edu.sgu.www.mhxysy.entity.xingshi.Xingshi;
import cn.edu.sgu.www.mhxysy.enums.InlayStatus;
import cn.edu.sgu.www.mhxysy.function.computer.score.impl.XingshiScoreComputer;
import cn.edu.sgu.www.mhxysy.mapper.xingshi.XingshiMapper;
import cn.edu.sgu.www.mhxysy.pager.xingshi.XingshiPager;
import cn.edu.sgu.www.mhxysy.service.xingshi.XingshiService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import cn.edu.sgu.www.mhxysy.vo.except.AttributeVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class XingshiServiceImpl implements XingshiService {

	private final XingshiMapper xingshiMapper;
	private final XingshiScoreComputer xingshiScoreComputer;

	@Autowired
	public XingshiServiceImpl(
			XingshiMapper xingshiMapper,
			XingshiScoreComputer xingshiScoreComputer) {
		this.xingshiMapper = xingshiMapper;
		this.xingshiScoreComputer = xingshiScoreComputer;
	}

	@Override
	public void compute(String xingshiId) {
		UpdateWrapper<Xingshi> updateWrapper = new UpdateWrapper<>();

		updateWrapper.eq("id", xingshiId);
		updateWrapper.set("score", xingshiScoreComputer.compute(xingshiId));

		xingshiMapper.update(updateWrapper.getEntity(), updateWrapper);
	}

	@Override
	public void inlay(XingshiInlayDTO xingshiInlayDTO) {
		String xingshiId = xingshiInlayDTO.getXingshiId();
		String equipmentId = xingshiInlayDTO.getEquipmentId();

		// 修改星石状态
		UpdateWrapper<Xingshi> updateWrapper = new UpdateWrapper<>();

		updateWrapper.eq("id", xingshiId);

		if (StringUtils.isNotEmpty(equipmentId)) {
			updateWrapper.set("equipment_id", equipmentId);
			updateWrapper.set("inlay_status", InlayStatus.YXQ.getValue());

			updateWrapper.eq("inlay_status", InlayStatus.WXQ.getValue());
		} else {
			updateWrapper.set("equipment_id", null);
			updateWrapper.set("inlay_status", InlayStatus.WXQ.getValue());

			updateWrapper.eq("inlay_status", InlayStatus.YXQ.getValue());
		}

		xingshiMapper.update(updateWrapper.getEntity(), updateWrapper);
	}

	@Override
	public void insert(XingshiDTO xingshiDTO) {
		Xingshi xingshi = toEntity(xingshiDTO);

		xingshi.setId(xingshi.uuid());

		if (StringUtils.isNotEmpty(xingshi.getEquipmentId())) {
			xingshi.setInlayStatus(InlayStatus.YXQ.getValue());
		} else {
			xingshi.setInlayStatus(InlayStatus.WXQ.getValue());
		}

		xingshiMapper.insert(xingshi);
	}

	@Override
	public void deleteById(String id) {
		xingshiMapper.deleteById(id);
	}

	@Override
	public void updateById(XingshiDTO xingshiDTO) {
		ObjectUtils.requireNonNull(xingshiDTO.getId(), CustomMessages.ID_NOT_NULL);

		Xingshi xingshi = toEntity(xingshiDTO);

		if (StringUtils.isNotEmpty(xingshi.getEquipmentId())) {
			xingshi.setInlayStatus(InlayStatus.YXQ.getValue());
		} else {
			xingshi.setInlayStatus(InlayStatus.WXQ.getValue());
		}

		xingshiMapper.updateById(xingshi);
	}

	@Override
	public AttributeVO selectAttribute(String xingshiId) {
		return xingshiMapper.selectAttribute(xingshiId);
	}

	@Override
	public Page<Xingshi> selectByPage(XingshiPager pager) {
		QueryWrapper<Xingshi> queryWrapper = new QueryWrapper<>();
		Page<Xingshi> page = Pager.ofPage(pager);

		queryWrapper.eq(
				StringUtils.isNotEmpty(pager.getRoleId()),
				"role_id", pager.getRoleId()
		);
		queryWrapper.eq(
				pager.getCategoryId() != null,
				"category_id", pager.getCategoryId()
		);
		queryWrapper.eq(
				StringUtils.isNotEmpty(pager.getEquipmentId()),
				"equipment_id", pager.getEquipmentId()
		);

		// 得到order by语句
		String statement = Pager.getOrderByStatement(pager);
		
		if (StringUtils.isNotEmpty(statement)) {
			queryWrapper.last(statement);
		}

		return xingshiMapper.selectPage(page, queryWrapper);
	}

	@Override
	public Xingshi selectByEquipmentId(String equipmentId) {
		QueryWrapper<Xingshi> queryWrapper = new QueryWrapper<>();

		queryWrapper.eq("equipment_id", equipmentId);

		return xingshiMapper.selectOne(queryWrapper);
	}

	private Xingshi toEntity(XingshiDTO xingshiDTO) {
		Xingshi xingshi = new Xingshi();

		xingshi.setId(xingshiDTO.getId());
		xingshi.setGrade(xingshiDTO.getGrade());
		xingshi.setScore(xingshiDTO.getScore());
		xingshi.setRoleId(xingshiDTO.getRoleId());
		xingshi.setCategoryId(xingshiDTO.getCategoryId());
		xingshi.setEquipmentId(xingshiDTO.getEquipmentId());
		xingshi.setPropertyValue(xingshiDTO.getPropertyValue());

		return xingshi;
	}

}